﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.IO;
using Lamex.Base;
using System.Data.SqlClient;
using Lamex.Common;
using DevExpress.XtraVerticalGrid.Rows;
using DevExpress.XtraGrid.Views.Grid;
using System.Security.AccessControl;
using DevExpress.XtraGrid.Views.Base;

namespace Lamex.mQuality.UI
{
    public partial class frmQC_OQA_Defect_Check : frmBaseChild
    {
        string strSql;
        DataTable dt;
        DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit uc = null;
        string isFlag = "";
        string path = "";
        string rowName = "";
        string status = "";
        string targetpath = "";
        public frmQC_OQA_Defect_Check()
        {
            InitializeComponent();     
        }

        private void frmQC_OQA_Defect_Check_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            gridView1.OptionsBehavior.Editable = false;
            //文字左对齐，直接在设计器中设置
            //vGridControl1.Appearance.RecordValue.HAlignment = DevExpress.Utils.HorzAlignment.Near;

            uc = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
            uc.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            vGridControl1.Rows["rowIssueImage"].Properties.RowEdit = uc;

            if (Loginer.CurrentUser.Account.ToLower() == "tx02951")
                targetpath = @"C:\Program Files\Microsoft SQL Server\80\Tools\Binn";
            else
                targetpath = @"\\tx-fs8\tx-fs\TX-Public\TX-MFG\QC\03 OQC\OQA_Issue_Image";
        }

        private void frmQC_OQA_Defect_Check_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width - 330;
        }
       
        private void cboxProductLine_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboxIssueType.EditValue = null;
            cboxIssueType.Properties.Items.Clear();

            strSql = @"SELECT ISSUE_ITEM FROM QC_OQA_Issue_Type
WHERE PRODUCT_LINE='" + cboxProductLine.Text + @"'";

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            //cboxIssueType.Properties.NullText = "请选择...";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cboxIssueType.Properties.Items.Add(dt.Rows[i]["ISSUE_ITEM"].ToString());
            }
        }

        private void sbtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(isFlag)) return;
           
            if(string.IsNullOrEmpty(vGridControl1.Rows["rowISSUE_NO"].Properties.Value.ToString()))
            {
                XtraMessageBox.Show("记录编号不能为空!");
                return;
            }
            if (vGridControl1.Rows["rowCHECK_DATE"].Properties.Value!=null)
            {
                if (vGridControl1.Rows["rowCHECK_DATE"].Properties.Value.ToString() == "")
                {
                    XtraMessageBox.Show("检查日期不能为空！");
                    return;
                }
                    
            }
            else
            {
                XtraMessageBox.Show("检查日期不能为空！");
                return;
            }
            if(vGridControl1.Rows["rowPackage_Mo"].Properties.Value!=null)
            {
                if(vGridControl1.Rows["rowPackage_Mo"].Properties.Value.ToString()=="")
                {
                    XtraMessageBox.Show("MO编号不能为空!");
                    return;
                }
            }
            else
            {
                XtraMessageBox.Show("MO编号不能为空!");
                return;
            }

            if (vGridControl1.Rows["rowPART_NO"].Properties.Value != null)
            {
                if (vGridControl1.Rows["rowPART_NO"].Properties.Value.ToString()=="")
                {
                    XtraMessageBox.Show("部件编号不能为空!");
                    return;
                }
                    
            }
            else
            {
                XtraMessageBox.Show("部件编号不能为空!");
                return;
            }
            if (vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value != null)
            {
                if (vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value.ToString() == "")
                {
                    XtraMessageBox.Show("生厂线不能为空!");
                    return;
                }
                   
            }
            else
            {
                XtraMessageBox.Show("生厂线不能为空!");
                return;
            }
            if (vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value != null)
            {
                if (vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value.ToString() =="")
                {
                    XtraMessageBox.Show("问题类别不能为空!");
                    return;
                }
                    
            }
            else
            {
                XtraMessageBox.Show("问题类别不能为空!");
                return;
            }
            if (vGridControl1.Rows["rowISSUE_QTY"].Properties.Value!=null)
            {
                if (vGridControl1.Rows["rowISSUE_QTY"].Properties.Value.ToString() == "")
                {
                    XtraMessageBox.Show("问题数量不能为空!");
                    return;
                }
                    
            }
            else
            {
                XtraMessageBox.Show("问题数量不能为空!");
                return;
            }
            if (vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value != null)
            {
                if (vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value.ToString() == "")
                {
                    XtraMessageBox.Show("责任部门不能为空");
                    return;
                }
            }
            else
            {
                XtraMessageBox.Show("责任部门不能为空");
                return;
            }
            if (vGridControl1.Rows["rowUPDATE_USER"].Properties.Value != null)
            {
                if (vGridControl1.Rows["rowUPDATE_USER"].Properties.Value.ToString() == "")
                {
                    XtraMessageBox.Show("更新用户不能为空!");
                    return;
                }
            }
            else
            {
                XtraMessageBox.Show("更新用户不能为空!");
                return;
            }
            if (vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value != null)
            {
                if (vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value.ToString() == "")
                {
                    XtraMessageBox.Show("更新时间不能为空!");
                    return;
                }
                   
            }
            else
            {
                XtraMessageBox.Show("更新时间不能为空!");
                return;
            }
            
            if (isFlag=="new")
            {
                DataTable dtKey = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select * from [LamexMESManager].[dbo].[QC_OQA_Issue_Record] where ISSUE_NO='" + vGridControl1.Rows["rowISSUE_NO"].Properties.Value.ToString() + "'");
                if(dtKey.Rows.Count!=0)
                {
                    XtraMessageBox.Show("记录编号已存在！");
                    return;
                }
                byte[] bytes = null;
                if (vGridControl1.Rows["rowIssueImage"].Properties.Value!=null)
                    bytes = (byte[])vGridControl1.Rows["rowIssueImage"].Properties.Value;
                string sql = "";
                //if (bytes != null)
                     sql = @"insert into [LamexMESManager].[dbo].[QC_OQA_Issue_Record] ( 
                            [ISSUE_NO]
                          ,[CHECK_DATE]
                          ,[Package_Mo]
                          ,[Order_Number]
                          ,[DWG_NO]
                          ,[PART_NO]
                          ,[PRODUCT_LINE]
                          ,[ISSUE_QTY]
                          ,[ISSUE_ITEM]
                          ,[ISSUE_DESC]
                          ,[RESPONSIBLE_DEPT]
                          ,[CONFIRM_TIME]
                          ,[CONFIRM_USER]
                          ,[UPDATE_TIME]
                          ,[UPDATE_USER])
                    values(
                           '" + vGridControl1.Rows["rowISSUE_NO"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowCHECK_DATE"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowPackage_Mo"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowOrder_Number"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowDWG_NO"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowPART_NO"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowISSUE_QTY"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowISSUE_DESC"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value + @"'
                          , '" + vGridControl1.Rows["rowUPDATE_USER"].Properties.Value + @"'
                    )";
                //else
                //    sql = @"insert into [LamexMESManager].[dbo].[QC_OQA_Issue_Record] ( 
                //            [ISSUE_NO]
                //          ,[CHECK_DATE]
                //          ,[Package_Mo]
                //          ,[Order_Number]
                //          ,[DWG_NO]
                //          ,[PART_NO]
                //          ,[PRODUCT_LINE]
                //          ,[ISSUE_QTY]
                //          ,[ISSUE_ITEM]
                //          ,[ISSUE_DESC]
                //          ,[RESPONSIBLE_DEPT]
                //          ,[CONFIRM_TIME]
                //          ,[CONFIRM_USER]
                //          ,[UPDATE_TIME]
                //          ,[UPDATE_USER])
                //    values(
                //           '" + vGridControl1.Rows["rowISSUE_NO"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowCHECK_DATE"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowPackage_Mo"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowOrder_Number"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowDWG_NO"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowPART_NO"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowISSUE_QTY"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowISSUE_DESC"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value + @"'
                //          , '" + vGridControl1.Rows["rowUPDATE_USER"].Properties.Value + @"'
                //    )";
                //int m= Write(sql, bytes);
                int m = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql);
                if (m>0)
                {
                    Tools.CopyFile(path, targetpath, vGridControl1.Rows["rowISSUE_NO"].Properties.Value.ToString());
                    //\\tx-fs8\tx-fs\TX-Public\TX-MFG\QC\03 OQC\OQA_Issue_Image
                    XtraMessageBox.Show("成功保存！");
                    //使用File的静态方法删除路径下的一个文件
                    //File.Delete(file.FullName);
                    refreshGridViewData();
                    ClearList();
                    isFlag = "";
                    return;
                }
                else
                {
                    XtraMessageBox.Show("成功失败！");
                    return;
                }
            }
            else
            {
                byte[] bytes = null;
                if (!string.IsNullOrEmpty( vGridControl1.Rows["rowIssueImage"].Properties.Value.ToString()))
                    bytes = (byte[])vGridControl1.Rows["rowIssueImage"].Properties.Value;
                string sql = "";
                //,[ISSUE_ATTACHMENT]=@image
                //if (bytes!=null)
                  sql = @"update QC_OQA_Issue_Record set 
                               [CHECK_DATE]='" + vGridControl1.Rows["rowCHECK_DATE"].Properties.Value + @"'
                              ,[Package_Mo]='" + vGridControl1.Rows["rowPackage_Mo"].Properties.Value + @"'
                              ,[Order_Number]='" + vGridControl1.Rows["rowOrder_Number"].Properties.Value + @"'
                              ,[DWG_NO]='" + vGridControl1.Rows["rowDWG_NO"].Properties.Value + @"'
                              ,[PART_NO]='" + vGridControl1.Rows["rowPART_NO"].Properties.Value + @"'
                              ,[PRODUCT_LINE]='" + vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value + @"'
                              ,[ISSUE_QTY]='" + vGridControl1.Rows["rowISSUE_QTY"].Properties.Value + @"'
                              ,[ISSUE_ITEM]='" + vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value + @"'
                              ,[ISSUE_DESC]='" + vGridControl1.Rows["rowISSUE_DESC"].Properties.Value + @"'
                              ,[RESPONSIBLE_DEPT]='" + vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value + @"'
                              
                              ,[CONFIRM_TIME]='" + vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value + @"'
                              ,[CONFIRM_USER]='" + vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value + @"'
                              ,[UPDATE_TIME]='" + vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value + @"'
                              ,[UPDATE_USER]='" + vGridControl1.Rows["rowUPDATE_USER"].Properties.Value + @"'
                              where ISSUE_NO='" + vGridControl1.Rows["rowISSUE_NO"].Properties.Value + @"'";
                //else
                //    sql = @"update QC_OQA_Issue_Record set 
                //               [CHECK_DATE]='" + vGridControl1.Rows["rowCHECK_DATE"].Properties.Value + @"'
                //              ,[Package_Mo]='" + vGridControl1.Rows["rowPackage_Mo"].Properties.Value + @"'
                //              ,[Order_Number]='" + vGridControl1.Rows["rowOrder_Number"].Properties.Value + @"'
                //              ,[DWG_NO]='" + vGridControl1.Rows["rowDWG_NO"].Properties.Value + @"'
                //              ,[PART_NO]='" + vGridControl1.Rows["rowPART_NO"].Properties.Value + @"'
                //              ,[PRODUCT_LINE]='" + vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value + @"'
                //              ,[ISSUE_QTY]='" + vGridControl1.Rows["rowISSUE_QTY"].Properties.Value + @"'
                //              ,[ISSUE_ITEM]='" + vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value + @"'
                //              ,[ISSUE_DESC]='" + vGridControl1.Rows["rowISSUE_DESC"].Properties.Value + @"'
                //              ,[RESPONSIBLE_DEPT]='" + vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value + @"'                          
                //              ,[CONFIRM_TIME]='" + vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value + @"'
                //              ,[CONFIRM_USER]='" + vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value + @"'
                //              ,[UPDATE_TIME]='" + vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value + @"'
                //              ,[UPDATE_USER]='" + vGridControl1.Rows["rowUPDATE_USER"].Properties.Value + @"'
                //              where ISSUE_NO='" + vGridControl1.Rows["rowISSUE_NO"].Properties.Value + @"'";

                //int m = Write(sql, bytes);
                int m = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql);
                if (m > 0)
                {
                    Tools.CopyFile(path, targetpath, vGridControl1.Rows["rowISSUE_NO"].Properties.Value.ToString());
                    XtraMessageBox.Show("成功保存！");
                    refreshGridViewData();
                    ClearList();
                    status = "";
                    isFlag = "";
                    return;
                }
                else
                {
                    XtraMessageBox.Show("成功失败！");
                    return;
                }
            }
        }

        private void refreshGridViewData()
        {
            vGridControl1.OptionsBehavior.Editable = false;
            vGridControl1.DataSource = null;

            string strLine = string.IsNullOrEmpty(cboxProductLine.Text.ToString()) ? " 1=1" : "PRODUCT_LINE='" + cboxProductLine.Text.ToString() + "'";
            string strType = string.IsNullOrEmpty(cboxIssueType.Text.ToString()) ? " 1=1" : "ISSUE_ITEM='" + cboxIssueType.Text.ToString() + "'";
            string strDept = string.IsNullOrEmpty(cboxDutyDept.Text.ToString()) ? " 1=1" : "RESPONSIBLE_DEPT='" + cboxDutyDept.Text.ToString() + "'";
            string strDate = string.IsNullOrEmpty(dteCheckDate.Text.ToString()) ? " 1=1" : "CHECK_DATE='" + dteCheckDate.Text.ToString() + "'";

            strSql = @"SELECT ISSUE_NO AS 记录编号,CHECK_DATE AS 检查日期,Package_Mo AS MO编号,
            Order_Number AS 跟进表号,DWG_NO AS 图纸编号,PART_NO AS 部件编号,
            PRODUCT_LINE AS 生产线,ISSUE_QTY AS 问题数量,ISSUE_ITEM AS 问题类别,
            ISSUE_DESC AS 问题描述,RESPONSIBLE_DEPT AS 责任部门
            FROM QC_OQA_Issue_Record
            WHERE " + strLine + @"
            AND " + strType + @"
            AND " + strDept + @"
            AND " + strDate;

            gridControl1.DataSource = null;
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            if (dt.Rows.Count > 0)
            {
                gridControl1.DataSource = dt;
            }

            //遍历并设置自动列宽
            setGridViewColumnAutoWidth();

        }

        private void resetGridViewEditProperties()
        {
            vGridControl1.OptionsBehavior.Editable = true;
            vGridControl1.Rows["rowISSUE_NO"].Properties.AllowEdit = false;
            vGridControl1.Rows["rowUPDATE_USER"].Properties.AllowEdit = false;
            vGridControl1.Rows["rowUPDATE_TIME"].Properties.AllowEdit = false;
            vGridControl1.Rows["rowCONFIRM_USER"].Properties.AllowEdit = false;
            vGridControl1.Rows["rowPRODUCT_LINE"].Properties.AllowEdit = false;
            //vGridControl1.Rows["rowIssueImage"].Properties.AllowEdit = true;
           
            
            rowCHECK_DATE.Properties.AllowEdit = true;
            rowPackage_Mo.Properties.AllowEdit = true;
            rowPART_NO.Properties.AllowEdit = true;

            rowISSUE_ITEM.Properties.AllowEdit = true;
            rowISSUE_DESC.Properties.AllowEdit = true;
            rowISSUE_QTY.Properties.AllowEdit = true;
            rowRESPONSIBLE_DEPT.Properties.AllowEdit = true;

            rowIssueImage.Properties.AllowEdit = false;
        }

        private void setGridViewColumnAutoWidth()
        {
            gridView1.OptionsView.ColumnAutoWidth = false;
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.BestFitColumns();
                if (i == 2)
                {
                    gridView1.Columns[2].Width = 20;
                }
                else
                {
                    gridView1.Columns[i].BestFit();//自动列宽    
                }
            }
        }
        
        #region 初始化标准工具条

        public override void DoView(IButtonInfo sender)
        {
            labelControl2.Text = "记录内容(刷新)";
            ClearList();
            refreshGridViewData();
            base.DoView(sender);
        }

        public override void DoAdd(IButtonInfo sender)
        {
            labelControl2.Text = "记录内容(新增)";
            vGridControl1.DataSource = null;
            resetGridViewEditProperties();
           
            vGridControl1.Rows["rowISSUE_NO"].Properties.Value = DateTime.Now.ToString("yyyyMMddHHmmss");
            //vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value = Loginer.CurrentUser.AccountName;
            //vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value = DateTime.Now;

            vGridControl1.Rows["rowUPDATE_USER"].Properties.Value = Loginer.CurrentUser.AccountName;
            vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value = DateTime.Now;
            isFlag = "new";
            base.DoAdd(sender);
        }
        private void ClearList()
        {
            for (int i = 0; i < vGridControl1.Rows.Count; i++)
            {
                vGridControl1.SetCellValue(vGridControl1.Rows[i], i, null);
            }
        }
        public override void DoEdit(IButtonInfo sender)
        {
            labelControl2.Text = "记录内容(修改)";
            status = "";
            resetGridViewEditProperties();
            vGridControl1.Rows["rowUPDATE_USER"].Properties.Value = Loginer.CurrentUser.AccountName;
            vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value = DateTime.Now;
            
            isFlag = "edit";
            base.DoEdit(sender);
        }

        public override void DoSave(IButtonInfo sender)
        {
            labelControl2.Text = "记录内容(保存)";
            sbtnSave_Click(null, null);
            base.DoSave(sender);
        }

        #endregion

        //GRIDVIEW表数据联动
        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            GridHitInfo hint = gridView1.CalcHitInfo(e.X, e.Y);
            if (gridView1.RowCount == 0 || /*记录数大于0*/
                    !hint.InRowCell /*有效的单元格*/||
                e.Button != MouseButtons.Left /*鼠标左键*/||
                    e.Clicks != 1  /*单击*/
                )
            {
                return;
            }
            gridView1.FocusedRowHandle = hint.RowHandle;

            DataRow dr = gridView1.GetDataRow(gridView1.FocusedRowHandle);//获取选中行
            string strSN = gridView1.GetFocusedRowCellValue("记录编号").ToString();//获取某一行的指定列的值
            
            //刷新当前检查记录数据
            strSql = @"select 
 [ISSUE_NO]
      ,[CHECK_DATE]
      ,[Package_Mo]
      ,[Order_Number]
      ,[DWG_NO]
      ,[PART_NO]
      ,[PRODUCT_LINE]
      ,[ISSUE_QTY]
      ,[ISSUE_ITEM]
      ,[ISSUE_DESC]
      ,[RESPONSIBLE_DEPT]
      --,[ISSUE_ATTACHMENT]
      ,[CONFIRM_TIME]
      ,[CONFIRM_USER]
      ,[UPDATE_TIME]
      ,[UPDATE_USER],
	  CONVERT(image,ISSUE_ATTACHMENT) ISSUE_ATTACHMENT
 FROM (
SELECT 
 [ISSUE_NO]
      ,[CHECK_DATE]
      ,[Package_Mo]
      ,[Order_Number]
      ,[DWG_NO]
      ,[PART_NO]
      ,[PRODUCT_LINE]
      ,[ISSUE_QTY]
      ,[ISSUE_ITEM]
      ,[ISSUE_DESC]
      ,[RESPONSIBLE_DEPT]
      --,[ISSUE_ATTACHMENT]
      ,[CONFIRM_TIME]
      ,[CONFIRM_USER]
      ,[UPDATE_TIME]
      ,[UPDATE_USER],
	 
 null as ISSUE_ATTACHMENT
                    FROM QC_OQA_Issue_Record
                    WHERE ISSUE_NO='"+ strSN + @"') m ";

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            string strstart1 = DateTime.Now.ToString();
            uc = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
            uc.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            vGridControl1.Rows["rowIssueImage"].Properties.RowEdit = uc;

            vGridControl1.DataSource = dt;
            //string jpgpath = targetpath ;
            string[] strArray = Directory.GetFiles(targetpath);
            string fileName = strArray.Where(p => p.Contains(strSN)).FirstOrDefault();
            if (File.Exists(fileName))
            {
                Image img = null;
                img = Image.FromFile(fileName);
                vGridControl1.Rows["rowIssueImage"].Properties.Value = GetByteImage(img);
                //GetPictureData(fileName);

                //GetByteImage(img);
            }
            else
            {
                vGridControl1.Rows["rowIssueImage"].Properties.Value = null;
                string strstart2 = DateTime.Now.ToString();
                
            }

            //resetGridViewEditProperties();
            vGridControl1.OptionsBehavior.Editable = false;//只有双击、新增、修改时才可编辑，光标移动与单击均锁定

            status = vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value.ToString();
            //vGridControl1.Rows["rowUPDATE_USER"].Properties.Value = Loginer.CurrentUser.AccountName;
            //vGridControl1.Rows["rowUPDATE_TIME"].Properties.Value = DateTime.Now;

            //isFlag = "edit";
            //vGridControl1.Rows["rowIssueImage"].Appearance
            //uc = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
            //uc.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            //vGridControl1.Rows["rowIssueImage"].Properties.RowEdit = uc;
            //vGridControl1.OptionsBehavior.Editable = false;
        }
        //将image转化为二进制           
        public  byte[] GetByteImage(Image img)
        {
            byte[] bt = null;
            if (!img.Equals(null))
            {
                using (MemoryStream mostream = new MemoryStream())
                {
                    Bitmap bmp = new Bitmap(img);
                    bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Bmp);//将图像以指定的格式存入缓存内存流

                    bt = new byte[mostream.Length];
                    mostream.Position = 0;//设置留的初始位置
                    mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
                }
            }
            return bt;
        }
        private void vGridControl1_RowChanged(object sender, DevExpress.XtraVerticalGrid.Events.RowChangedEventArgs e)
        {
            //XtraMessageBox.Show("Heal");
        }
        
        private void vGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {            
            if (e.Row.Name== "rowPackage_Mo")
            {
                DataSet ds = Lamex.Base.BaseBuildSql.GetDataSetBySql(" select * from Transfer_Part_Detailed where Package_Mo='" + e.Value + "' ;select * from Transfer_Package_List where Package_Mo='"+ e.Value + "'");
                
                DevExpress.XtraEditors.Repository.RepositoryItemComboBox comb = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                if (ds.Tables[0].Rows.Count!=0)
                {
                    
                    comb.Name = "comb";
                    foreach(DataRow dr in ds.Tables[0].Rows)
                    {
                        comb.Items.Add(dr["PART_NO"].ToString());
                    }
                    
                    vGridControl1.Rows["rowPART_NO"].Properties.RowEdit = comb;
                }
                else
                {
                    comb.Items.Clear();
                    vGridControl1.Rows["rowPART_NO"].Properties.RowEdit = comb;
                    vGridControl1.Rows["rowPART_NO"].Properties .Value= "";
                }
                if (ds.Tables[1].Rows.Count != 0)
                {
                    vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value = ds.Tables[1].Rows[0]["PROD_LINE"];

                    DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select ISSUE_ITEM from QC_OQA_Issue_Type where PRODUCT_LINE='" + vGridControl1.Rows["rowPRODUCT_LINE"].Properties.Value + "' ");
                    DevExpress.XtraEditors.Repository.RepositoryItemComboBox combItem = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                    if (dt.Rows.Count != 0)
                    {

                        comb.Name = "comb1";
                        foreach (DataRow dr in dt.Rows)
                        {
                            combItem.Items.Add(dr["ISSUE_ITEM"].ToString());
                        }

                        vGridControl1.Rows["rowISSUE_ITEM"].Properties.RowEdit = combItem;
                    }
                    else
                    {
                        combItem.Items.Clear();
                        vGridControl1.Rows["rowISSUE_ITEM"].Properties.RowEdit = combItem;
                        vGridControl1.Rows["rowISSUE_ITEM"].Properties.Value = "";
                    }
                }
            }

            if (e.Row.Name == "rowPRODUCT_LINE")
            {
                
            }

            if (e.Row.Name == "rowRESPONSIBLE_DEPT")
            {
                if (vGridControl1.Rows["rowISSUE_NO"].Properties.Value != null)
                {
                    DataTable dtKey = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select * from [LamexMESManager].[dbo].[QC_OQA_Issue_Record] where ISSUE_NO='" + vGridControl1.Rows["rowISSUE_NO"].Properties.Value.ToString() + "'");
                    if (dtKey.Rows.Count != 0)
                    {
                        if (status != vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value.ToString())
                        {
                            vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value = DateTime.Now; ;
                            vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value = Loginer.CurrentUser.AccountName;
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vGridControl1.Rows["rowRESPONSIBLE_DEPT"].Properties.Value.ToString()))
                        {
                            vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value = DateTime.Now; ;
                            vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value = Loginer.CurrentUser.AccountName;
                        }
                        else
                        {
                            vGridControl1.Rows["rowCONFIRM_TIME"].Properties.Value = "";
                            vGridControl1.Rows["rowCONFIRM_USER"].Properties.Value = "";
                        }
                    }
                }
                
            }
            DataTable dtDetail = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select * from Transfer_Part_Detailed where Package_Mo='" + vGridControl1.Rows["rowPackage_Mo"].Properties.Value + "' and Part_No='"+ vGridControl1.Rows["rowPART_NO"].Properties.Value + "' ");
            if(dtDetail.Rows.Count!=0)
            {
                vGridControl1.Rows["rowDWG_NO"].Properties.Value = dtDetail.Rows[0]["Drawing_No"].ToString();
                vGridControl1.Rows["rowOrder_Number"].Properties.Value = dtDetail.Rows[0]["Order_Number"].ToString();
            }
            

        }

        private void vGridControl1_DoubleClick(object sender, EventArgs e)
        {
            //BaseRow selectRow = vGridControl1.FocusedRow;
            //if(selectRow.Name== "rowIssueImage")
            //LoadSelectPicture();
        }
        /// <summary>
        /// 加载选中图片
        /// </summary>

        public void LoadSelectPicture()
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = @"All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|"
                                + "Windows Bitmap(*.bmp)|*.bmp|"
                                + "Windows Icon(*.ico)|*.ico|"
                                + "Graphics Interchange Format (*.gif)|(*.gif)|"
                                + "JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|"
                                + "Portable Network Graphics (*.png)|*.png|"
                                + "Tag Image File Format (*.tif)|*.tif;*.tiff";
            openfile.ShowDialog();

            path = openfile.FileName;
            if (!File.Exists(path))
            {
                MessageBox.Show("没有选中图片！");
                return;
            }
            vGridControl1.Rows["rowIssueImage"].Properties.Value = GetPictureData(path);
        }
        /// <summary>
        /// image转化为字节
        /// </summary>
        /// <returns></returns>
        public byte[] GetPictureData(string imagepath)
        {
            ///**/////根据图片文件的路径使用文件流打开，并保存为byte[] 
            //FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 
            //byte[] byData = new byte[fs.Length];
            //fs.Read(byData, 0, byData.Length);
            //fs.Close();
            //return byData;
            return null;
        }
        /// <summary>
        /// 字节组转化图片image
        /// </summary>
        /// <param name="Bytes"></param>
        /// <returns></returns>
        private Image ByteArrayToImage(byte[] Bytes)
        {
            if (Bytes.Length != 0)
            {
                MemoryStream ms = new MemoryStream(Bytes);
                return Bitmap.FromStream(ms, true);
            }
            return null;
        }
        private int Write(string strSql, byte[] imageBytes)
        {
            string sConn = SqlConfiguration.CurrentConnectionString;

            using (SqlConnection conn = new SqlConnection(sConn))
            {
                using (SqlCommand cmd = new SqlCommand(strSql, conn))
                {
                    try
                    {
                        conn.Open();
                        
                        SqlParameter sqlParameter = new SqlParameter("@image", SqlDbType.Image);
                        sqlParameter.Value = imageBytes;
                        if(imageBytes != null)
                            cmd.Parameters.Add(sqlParameter);
                        int rows = cmd.ExecuteNonQuery();
                        conn.Close();
                        return rows;
                    }
                    catch (Exception e)
                    {
                        throw e;
                        //return 0;
                    }
                }
            }
        }

        private void gridControl1_EditorKeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.C)
            {
                Clipboard.SetDataObject(gridView1.GetFocusedRowCellValue(gridView1.FocusedColumn));
                e.Handled = true;
            }
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            labelControl2.Text = "记录内容(刷新)";

            vGridControl1.OptionsBehavior.Editable = false;

            rowCHECK_DATE.Properties.AllowEdit = false;
            rowPackage_Mo.Properties.AllowEdit = false;
            rowPART_NO.Properties.AllowEdit = false;

            rowISSUE_ITEM.Properties.AllowEdit = false;
            rowISSUE_DESC.Properties.AllowEdit = false;
            rowISSUE_QTY.Properties.AllowEdit = false;
            rowRESPONSIBLE_DEPT.Properties.AllowEdit = false;

            rowIssueImage.Properties.AllowEdit = false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            labelControl2.Text = "记录内容(修改)";
            resetGridViewEditProperties();
        }

        public override void DoExport(IButtonInfo sender)
        {
            base.DoExport(sender);
        }

        private void vGridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            BaseRow selectRow = vGridControl1.FocusedRow;
            if (selectRow.Name == "rowIssueImage")
                LoadSelectPicture();
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            DoView(null);
        }
    }
}